package com.ruoyi.controller.production;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.production.MesProductionPicking;
import com.ruoyi.service.production.IMesProductionPickingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 生产领料单Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/production/picking")
public class MesProductionPickingController extends BaseController {
    @Autowired
    private IMesProductionPickingService IMesProductionPickingService;

    /**
     * 查询生产领料单列表
     */
    @PreAuthorize("@ss.hasPermi('production:picking:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesProductionPicking mesProductionPicking) {
        startPage();
        List<MesProductionPicking> list = IMesProductionPickingService.selectMesProductionPickingList(mesProductionPicking);
        return getDataTable(list);
    }

    /**
     * 导出生产领料单列表
     */
    @PreAuthorize("@ss.hasPermi('production:picking:export')")
    @Log(title = "生产领料单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesProductionPicking mesProductionPicking) {
        List<MesProductionPicking> list = IMesProductionPickingService.selectMesProductionPickingList(mesProductionPicking);
        ExcelUtil<MesProductionPicking> util = new ExcelUtil<MesProductionPicking>(MesProductionPicking.class);
        util.exportExcel(response, list, "生产领料单数据");
    }

    /**
     * 获取生产领料单详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:picking:query')")
    @GetMapping(value = "/{pickingId}")
    public AjaxResult getInfo(@PathVariable("pickingId") Long pickingId) {
        return success(IMesProductionPickingService.selectMesProductionPickingByPickingId(pickingId));
    }

    /**
     * 新增生产领料单
     */
    @PreAuthorize("@ss.hasPermi('production:picking:add')")
    @Log(title = "生产领料单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesProductionPicking mesProductionPicking) {
        // 校验领料单编号唯一性
        if (!IMesProductionPickingService.checkPickingCodeUnique(mesProductionPicking.getPickingCode(), mesProductionPicking.getPickingId())) {
            return error("新增生产领料单失败，领料单编号已存在");
        }
        return toAjax(IMesProductionPickingService.insertMesProductionPicking(mesProductionPicking));
    }

    /**
     * 修改生产领料单
     */
    @PreAuthorize("@ss.hasPermi('production:picking:edit')")
    @Log(title = "生产领料单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesProductionPicking mesProductionPicking) {
        // 校验领料单编号唯一性（排除自身）
        MesProductionPicking existPicking = IMesProductionPickingService.selectMesProductionPickingByPickingId(mesProductionPicking.getPickingId());
        if (existPicking != null && !existPicking.getPickingCode().equals(mesProductionPicking.getPickingCode())) {
            if (!IMesProductionPickingService.checkPickingCodeUnique(mesProductionPicking.getPickingCode(), mesProductionPicking.getPickingId())) {
                return error("修改生产领料单失败，领料单编号已存在");
            }
        }
        return toAjax(IMesProductionPickingService.updateMesProductionPicking(mesProductionPicking));
    }

    /**
     * 删除生产领料单
     */
    @PreAuthorize("@ss.hasPermi('production:picking:remove')")
    @Log(title = "生产领料单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{pickingIds}")
    public AjaxResult remove(@PathVariable Long[] pickingIds) {
        return toAjax(IMesProductionPickingService.deleteMesProductionPickingByPickingIds(pickingIds));
    }

    /**
     * 校验领料单编号是否唯一
     */
    @GetMapping("/checkPickingCodeUnique")
    public AjaxResult checkPickingCodeUnique(String pickingCode, Long pickingId) {
        boolean isUnique = IMesProductionPickingService.checkPickingCodeUnique(pickingCode, pickingId);
        return AjaxResult.success(isUnique);
    }

    /**
     * 根据工单ID查询领料单列表
     */
    @PreAuthorize("@ss.hasPermi('production:picking:list')")
    @GetMapping("/listByWorkOrderId/{workOrderId}")
    public AjaxResult listByWorkOrderId(@PathVariable Long workOrderId) {
        List<MesProductionPicking> list = IMesProductionPickingService.selectPickingListByWorkOrderId(workOrderId);
        return success(list);
    }

    /**
     * 审核生产领料单
     */
    @PreAuthorize("@ss.hasPermi('production:picking:audit')")
    @Log(title = "生产领料单审核", businessType = BusinessType.UPDATE)
    @PutMapping("/audit/{pickingId}/{status}")
    public AjaxResult audit(@PathVariable Long pickingId, @PathVariable String status) {
        return toAjax(IMesProductionPickingService.auditMesProductionPicking(pickingId, status));
    }

    /**
     * 执行生产领料单
     */
    @PreAuthorize("@ss.hasPermi('production:picking:execute')")
    @Log(title = "生产领料单执行", businessType = BusinessType.UPDATE)
    @PutMapping("/execute/{pickingId}")
    public AjaxResult execute(@PathVariable Long pickingId) {
        return toAjax(IMesProductionPickingService.executeMesProductionPicking(pickingId));
    }
}